package checkDetail;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;
import weaver.conn.RecordSet;
import general.BaseBean;

public class WorkflowTempCodeCheck extends BaseBean implements CheckBase  {

    @Override
    public JSONObject checkData(HashMap dataMap) {
        setRecordSet();
        (new BaseBean()).writeLog("WorkflowTempCodeCheck checkData start");
        //定义查询sql
        String checkSql = "select a.id as htmlid, a.formid, a.nodeid, a.workflowid, a.isbill, a.type, a.layoutname, a.syspath, b.workflowname from workflow_nodehtmllayout a, workflow_base b where a.workflowid = b.id and a.version in (0,1) ";
        recordSet.executeSql(checkSql);
        List resultList = new ArrayList();
        String result = "";
        //重新实例化一个rs
        RecordSet rs1 = new RecordSet();
        while(recordSet.next()){
            //读取syspath文件内容并检测内容
            String formId = recordSet.getString("formid");
            String nodeId = recordSet.getString("nodeid");
            String workflowId = recordSet.getString("workflowid");
            String isbill = recordSet.getString("isbill");
            String type = recordSet.getString("type");
            String htmlName = recordSet.getString("layoutname");
            String sysPath = recordSet.getString("syspath");
            String workflowName = recordSet.getString("workflowname");
            
            //读取sysPath文件内容
            String htmlContent = readFile(sysPath).toLowerCase();
            //String htmlContent = readFile("E:\\user\\weaverWorkspace\\ecology80\\filesystem\\htmllayout\\htmllay82_382_0.html");
            
            //表单内容中包含其他的<form>标签
            if(htmlContent.contains("<form") || htmlContent.contains("</form>"))
            {
                Map resultMap = new HashMap<String, String>();
                resultMap.put("showMessage", formatMessage(workflowName, "路径中E7模板：" + htmlName + " 源码出现标签使用错误的情况，导致表单被提前结束，请手动修复！"));
                resultMap.put("linkurl","/workflow/html/LayoutEditFrame.jsp?formid=" + formId + "&wfid=" + workflowId + "&nodeid=" + nodeId + "&isbill=" + isbill + "&layouttype=" + type + "&ajax=0&modeid=0");
                resultMap.put("linkname","点击此处进行修改。");
                resultMap.put("workflowid",workflowId);
                resultList.add(resultMap);
            }else if(!htmlContent.contains("</table>") && !htmlContent.contains("</tr>") && !htmlContent.contains("</td>"))
            {
                break;
            }else{
                int lastIndexTable = htmlContent.lastIndexOf("</table>");
                int lastIndexTr = htmlContent.lastIndexOf("</tr>");
                int lastIndexTd = htmlContent.lastIndexOf("</td>");
                
                if(lastIndexTable < lastIndexTr || lastIndexTr < lastIndexTd || lastIndexTable == -1 || lastIndexTr == -1 || lastIndexTd == -1)
                {
                    Map resultMap = new HashMap<String, String>();
                    resultMap.put("showMessage", formatMessage(workflowName, "路径中E7模板：" + htmlName + " 源码出现form标签再次使用的情况，导致表单被提前结束，请手动修复！"));
                    resultMap.put("linkurl","/workflow/html/LayoutEditFrame.jsp?formid=" + formId + "&wfid=" + workflowId + "&nodeid=" + nodeId + "&isbill=" + isbill + "&layouttype=" + type + "&ajax=0&modeid=0");
                    resultMap.put("linkname","点击此处进行修改。");
                    resultMap.put("workflowid",workflowId);
                    resultList.add(resultMap);
                }
            }
        }
        
        if(resultList.size() == 0){
            result = resultOk;
            resultList.add("没有检测到E7源码表单中form标签被提前结束掉。");
        }else{
            result = resultNeedFix;
        }
        (new BaseBean()).writeLog("WorkflowCustompageCheck checkData end");
        return formatReturnJsonCheck(result, resultList, this);
    }
    

    
    /**
     * 读取文件内容
     * @param fileName 文件路径
     * @return fileContent 文件内容
     */
    public static String readFile(String fileName) {
        String output = ""; 
        File file = new File(fileName);
        if(file.exists()){
          if(file.isFile()){
            try{
              BufferedReader input = new BufferedReader (new FileReader(file));
              StringBuffer buffer = new StringBuffer();
              String text;
              while((text = input.readLine()) != null)
                buffer.append(text +"/n");
              output = buffer.toString();          
            }
            catch(IOException ioException){
               (new BaseBean()).writeLog("File Error!");
            }
          }
          else if(file.isDirectory()){
              (new BaseBean()).writeLog(fileName + "is Directory!");
          }
        }
        else{
            (new BaseBean()).writeLog(fileName + "Does not exist!");
        }
        return output;
    }

    @Override
    public JSONObject fixData(HashMap dataMap) {
        return checkData(new HashMap());
    }

    @Override
    public String getClassDesc() {
        return "检测E7源码表单中form标签是否被提前结束掉";
    }

    @Override
    public String getClassName() {
        return "流程模板表单代码检测";
    }

}
